package mi.controls.highlight.impl
{
	import mi.controls.highlight.*;
		
	/**
	 * 语法高亮的SQL实现
	 * 
	 * @author liushimin 2011-7-5
	 * 
	 */	
	public class SqlHightLightImpl implements IHightLight  
	{
		/*子类必须实现本方法,获取高亮替换,元素为 { regex:正则 , match:替换 } */
		public function get regexList():Array{
			return _regexList;
		}
		
		/*获取当前高亮实现的别名用于外部调用*/
		public function get alias():String{
			return "SQL";
		}
		
		/** 
		 * 单行注释 
		 */ 
		private static var SINGLE_COMMENT:String = "--[^\\r\\n]*+"; 
		
		/** 
		 * 组合关键词表达式 
		 */ 
		private static var  KEYWORDS:String = "\\b(?:absolute|action|add|after|alter|as|asc|at|authorization|"+
				"begin|bigint|binary|bit|by|cascade|char|character|check|checkpoint|close|collate|"+
				"column|commit|committed|connect|connection|constraint|contains|continue|"+
				"create|cube|current|current_date|current_time|cursor|database|date|"+
				"deallocate|dec|decimal|declare|default|delete|desc|distinct|double|drop|"+
				"dynamic|else|end|end-exec|escape|except|exec|execute|false|fetch|first|"+
				"float|for|force|foreign|forward|free|from|full|function|global|goto|grant|"+
				"group|grouping|having|hour|ignore|index|inner|insensitive|insert|instead|"+
				"int|integer|intersect|into|is|isolation|key|last|level|load|local|max|min|"+
				"minute|modify|move|name|national|nchar|next|no|numeric|of|off|on|only|"+
				"open|option|order|out|output|partial|password|precision|prepare|primary|"+
				"prior|privileges|procedure|public|read|real|references|relative|repeatable|"+
				"restrict|return|returns|revoke|rollback|rollup|rows|rule|schema|scroll|"+
				"second|section|select|sequence|serializable|set|size|smallint|static|"+
				"statistics|table|temp|temporary|then|time|timestamp|to|top|transaction|"+
				"translation|trigger|true|truncate|uncommitted|union|unique|update|values|"+
				"varchar|varying|view|when|where|with|work|" +
				"Abs|Acos|Add_months|Ascii|Atan|Atan2|avg|"+
				"BFilename|Ceil|CharToRowid|Chr|Concat|Convert|Cos|Count|"+
				"Deref|Dump|Empty_BLOB|Empty_CLOB|Exp|Floor|Greatest|Grouping|"+
				"HexToRaw|InitCap|Instr|Instrb|Last_day|Least|Length|Lengthb|Ln|Log|Lower|LPad|LTrim|"+
				"Make_ref|Max|Min|Mod|Months_between|New_time|Next_day|NLS_charset_decl_len|"+
				"NLS_charset_id|NLS_charset_name|NLS_initcap|NLS_lower|NLS_upper|NLSSort|Nvl|"+
				"Power|RawToHex|Ref|RefToHex|Replace|Round|RowidToChar|RPad|RTrim|"+
				"Sign|Sin|Sinh|Soundex|Sqrt|Stddev|Substr|Substrb|Sum|SYS_context|SYS_guid|Sysdate|"+
				"Tan|Tanh|To_char|To_date|To_LOB|To_multi_byte|To_number|To_single_byte|Translate|Trim|Trunc|"+
				"Uid|Upper|User|Userenv|Value|Variance|VSize|"+
				"all|and|any|between|exist|in|join|left|like|not|null|or|outer|right)\\b"; 
				
		/** 
		 * 匹配字符的正则表达式 
		 */     
		private static var REGEX_QUOTE_CHAR:RegExp = new RegExp( 
			"(" + HLConstant.HIGHLIGHT_HTML_TAG + "|" + SINGLE_COMMENT + "|" + HLConstant.MULTI_COMMENT + ")|" + "(" + HLConstant.QUOTE_CHAR + ")","g"       
		); 
		
		/** 
		 * 匹配多行注释的正则表达式 
		 */ 
		private static var REGEX_MULTI_COMMENT:RegExp = new RegExp( 
			"(" + HLConstant.HIGHLIGHT_HTML_TAG + "|" + SINGLE_COMMENT + ")|" + "(" + HLConstant.MULTI_COMMENT + ")"  ,"gm"     
		); 
		
		/** 
		 * 匹配单行注释的正则表达式 
		 */ 
		private static var REGEX_SINGLE_COMMENT:RegExp = new RegExp( 
			"(" + HLConstant.HIGHLIGHT_HTML_TAG + "|" + HLConstant.MULTI_COMMENT + ")|" + "(" + SINGLE_COMMENT + ")" ,"g"      
		); 
		
		/** 
		 * 匹配 sql 关键词的正则表达式 
		 */ 
		private static var REGEX_KEYWORDS:RegExp = new RegExp( 
			"(" + HLConstant.HIGHLIGHT_HTML_TAG + "|" + HLConstant.MULTI_COMMENT + "|" + SINGLE_COMMENT + ")|" + "(" + KEYWORDS + ")","ig"   
		); 
		
		/** 
		 * 匹配 数字
		 */ 
		private static var REGEX_NUMBER:RegExp = new RegExp(
			"(" + HLConstant.HIGHLIGHT_HTML_TAG + "|" + HLConstant.MULTI_COMMENT + "|" + SINGLE_COMMENT + ")|" + "\\b([\\d]+(\\.[\\d]+)?|0x[a-f0-9]+)\\b","g"
		);
		
		private static var _regexList:Array=[
			/*Number*/
			{
				regex:REGEX_NUMBER,
				match:function($0:String,$1:String,$2:String,$3:String,pos:int,all:String):String{
					if($2==""){
						return $1;
					}
					else{
						return $1+"<font color=\"#AA3366\">"+$2+"</font>";
					}
				}
			},
			/*SingleQuoted*/
			{
				regex:REGEX_QUOTE_CHAR,
				match:function($0:String,$1:String,$2:String,pos:int,all:String):String{
					if($2==""){
						return $1;
					}
					else{
						return $1+"<font color=\"#0000FF\">"+$2.replace(/(\r)/g,"</font>$&<font color=\"#0000FF\">")+"</font>";
					}
				}
			},
			/*MultiLineComment*/
			{
				regex:REGEX_MULTI_COMMENT,
				match:function($0:String,$1:String,$2:String,pos:int,all:String):String{
					if($2==""){
						return $1;
					}
					else{
						return $1+"<font color=\"#FF0000\">"+$2.replace(/(\r)/g,"</font>$&<font color=\"#FF0000\">")+"</font>";
					}
				}
			},
			/*SingleLineComment*/
			{
				regex:REGEX_SINGLE_COMMENT,
				match:function($0:String,$1:String,$2:String,pos:int,all:String):String{
					if($2==""){
						return $1;
					}
					else{
						return $1+"<font color=\"#FF0000\">"+$2+"</font>";
					}
				}
			},
			/*关键字替换*/
			{
				regex:REGEX_KEYWORDS,
				match:function($0:String,$1:String,$2:String,pos:int,all:String):String{
					if($2==""){
						return $1;
					}
					else{
						return $1+"<b><font color=\"#008000\">"+$2+"</font></b>";
					}
				}
			}
		];
		
	}
}